package com.xm_jd.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xm_jd.model.dao.LoginDao;
import com.xm_jd.model.entitys.user;
import com.xm_jd.utils.BaseServlet;
import com.xm_jd.utils.MD5;

/**
 * Servlet implementation class Loginse
 */
@WebServlet("/loginse")
public class Loginse extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Loginse() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		user u = (user) request.getSession().getAttribute("users");
		if(u==null){
			this.forward("login");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String yming=request.getParameter("ming");
		String ymima=request.getParameter("mima");
		LoginDao lo=new LoginDao();
		PrintWriter out =response.getWriter();
		//将密码进行转码
		try {
			String newpwd = MD5.md5(ymima);
			if("".equals(yming)||"".equals(newpwd)){
				out.write("账号和密码不能为空");
			}else{
				List<user> list=lo.denglu(yming, newpwd);
				if(list.size()>0){
					request.getSession().setAttribute("users", list.get(0));
					//�ض�����ҳ
					//response.sendRedirect("Sy_lb");
					this.redirect("/Sy_lb");
				}else{
					request.setAttribute("error", "账号或密码错误");
					//request.getRequestDispatcher("jsps/login.jsp").forward(request, response);
					this.forward("login");
				}
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
